<?php
class Shopping_Model_Category extends Zend_Db_Table{
	protected $_name 	= 'product_category';
	protected $_primary = 'id';
	
	
	public function listCategory($arrParam =null, $options = null){
		if($options['task']=='list-category'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						->from('product_category',array('id','name','parents'))
						->where('status = 1')
						->order('order ASC');
			$result  = $db->fetchAll($select);
		}
		return $result;
	}
	public function itemInSelectbox($arrParam =null, $options = null){
		$db = Zend_Registry::get('connectDb');
		if($options == null){
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						 ->order('pc.order ASC');
			$result = $db->fetchAll($select);
		}
		if($options['task']=='admin-edit'){
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						 ->where('id != ?',$arrParam['id'],'INTEGER')
						 ->order('pc.order ASC');
			$result = $db->fetchAll($select);
		}
		$system = new Zendvn_System_Recursive($result);
		$result = $system->buildArray(0);
		$tmp = array('id'=>0,'name'=>'Root category','level'=>1,'order'=>1,'parents'=>0);
		array_unshift($result,$tmp);
		return $result;
	}
	public function listItem($arrParam =null, $options = null){
		$ssFilter = $arrParam['ssFilter'];		
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options['task']== 'admin-list'){
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by','modified_by'))
						 ->joinLeft('users AS u','u.id = pc.created_by',array('user_name'))
						 ->order('pc.order ASC');
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('pc.name LIKE ?',$keywords,'STRING');
				$result = $db->fetchAll($select);
			}
			if(empty($ssFilter['keywords'])){
				$row    = $db->fetchAll($select);
				$system = new Zendvn_System_Recursive($row);
				$result = $system->buildArray(0);
			}
		}
		return $result;
	}
	public function saveItem($arrParam=null,$options=null){		
		if($options['task']== 'admin-add'){
			$row = $this->fetchNew();
			$row['name'] 		= $arrParam['name'];
			if(!empty($arrParam['picture'])){
				$row['picture'] 		= $arrParam['picture'];
			}
			$row['parents'] 		= $arrParam['parents'];
			$row['order'] 	= $arrParam['order'];
			$row['status'] 			= $arrParam['status'];
			$row['created'] 		= date("Y-m-d");
			
			$info =  new Zendvn_System_Info();
			$created_by = $info->getMemberInfo('id');
			$row['created_by'] 	= $created_by;
			$row->save();
		}
		if($options['task']== 'admin-edit'){
			echo '<br>'. __METHOD__;
			$where = 'id = '. $arrParam['id'];
			$row = $this->fetchRow($where);
			if(!empty($arrParam['picture'])){
				$row->picture 			= $arrParam['picture'];
			}
			
			$row->name 		= $arrParam['name'];
			$row->parents 		= $arrParam['parents'];
			$row->status 	= $arrParam['status'];
			$row->order 			= $arrParam['order'];
			
			$info =  new Zendvn_System_Info();
			$modified_by = $info->getMemberInfo('id');
			$row->modified_by 		= $modified_by;		
				
			$row->modified 			= date("Y-m_d");
			$row->save();
		}
	}
	public function getItem($arrParam=null,$options=null){
		if($options['task']== 'admin-info'||$options['task']== 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
						 ->from('product_category AS pc')
						 ->where('id = ?',$arrParam['id']);
			$row = $db->fetchAll($select);
		}
		return $row[0];
	}
	public function deleteItem($arrParam=null,$options=null){
		if($options['task']== 'admin-delete'){
			$id = $arrParam['id'];
			$db = Zend_Registry::get('connectDb');
			/////////////////
			$select = $db->select()
						 ->from('product_category AS pc',array('id','name','status','parents','order','created_by'));
			$result1  = $db->fetchAll($select);
			///////////////////
			$select = $db->select()
						->from('product_category AS pc',array('id','name','status','parents','order','created_by'))
						->where('id = ?',$id,INTEGER);
			$result2 = $db->fetchAll($select);
			//////////////////////////			
			$system = new Zendvn_System_Recursive($result1);		
			$result = $system->buildArray($id);	
			if(count($result)>0){
				array_unshift($result,$result2[0]);
			}else{
				$result = $result2;
			}
			///////////////
			foreach ($result as $key => $value){
				$where = 'id = '. $value['id'];
				$this->delete($where);
			}
		}
		
		if($options['task']== 'admin-multidelete'){
			$cid = $arrParam['cid'];
			$db = Zend_Registry::get('connectDb');
			$newArr = array();
			if(count($cid)>0){
				foreach ($cid as $key=>$value){
					$select = $db->select()
								 ->from('product_category')
								 ->where('id =?',$value,INTEGER);
					$row = $db->fetchRow($select);
					$newArr[]=$row;
				}
				foreach ($newArr as $key1=>$value1) {
					$select = $db->select()
								 ->from('product_category');
					$row = $db->fetchAll($select);
					$system = new Zendvn_System_Recursive($row);
					$tmp 	= $system->buildArray($value1['id']);
					if(count($tmp)>0){
						foreach ($tmp as $keyTmp=>$valueTmp){
							array_unshift($newArr,$valueTmp);
						}
					}
				}
				foreach ($newArr as $keyNew =>$valNew){
					$where = 'id = '. $valNew['id'];
					$this->delete($where);
				}
			
			}
		}
	}
	public function changeStatus($arrParam=null,$options=nul){
		if($options['task']== 'admin-status'){	
			$cid = $arrParam['cid'];
			if(count($cid)>0){
				if($arrParam['type']==1){
					$status =1 ;
				}else{
					$status =0 ;
				}
				$ids = implode(',',$cid);
				$data = array('status'=>$status);
				$where = 'id IN ('.$ids.')';
				$this->update($data,$where);	
			}
			if($arrParam['id']>0){
				if($arrParam['type'] ==1){
					$status =1 ;
				}else{
					$status =0 ;
				}
				$data = array('status'=>$status);
				$where = 'id ='. $arrParam['id'];
				$this->update($data,$where);
			}
		}
		
	
	}
	public function sortItem($arrParam =null, $options = null){
		$cid 	= $arrParam['cid'];
		$order  = $arrParam['order'];
		if(count($cid)>0){
			foreach ($cid as $key=>$value){
				$data  = array('order'=>$order[$value]);
				$where = 'id = '. $value;
				$this->update($data,$where);
			}
		}
	}
}